﻿using System;
using System.Collections.Generic;
using System.Threading;
using Yxinmt.Controls;
using Yxinmt.Interface;
using Yxinmt.Utility;

namespace Yxinmt.Windows.Meeting
{
    public class CreateRecord : BaseWindow, IWindow
    {
        Entity.FileInfo _File { get; set; }
        DocumentViewing _DocumentViewing { get; set; }

        public CreateRecord(System.Windows.Window form, IWindow parentForm, System.Windows.Controls.Grid pagePanel, Dictionary<string, object> parameters)
        {
            Window = form;
            ParentWindow = parentForm;
            _PagePanel = pagePanel;
            _Parameters = parameters;
            _DocumentViewing = ParentWindow as DocumentViewing;
            _File = parameters.GetValue<Entity.FileInfo>("File");
            InitSet();

            initView();

            this.SetWindow();

            initLoad();

            _CreateRecordlist.Add(this);
        }

        static List<CreateRecord> _CreateRecordlist { get; set; }

        static CreateRecord()
        {
            _CreateRecordlist = new List<CreateRecord>();
        }

        public static void Dispose()
        {
            _CreateRecordlist.ForEach(c => c.CloseWindow());
        }

        void initView()
        {
            var webview1 = Utility.Common.GetProperty(_View, "WebView1");
            webview1["Url"] = $"{_DocumentViewing.ClientConfig.ServiceUrl}/ueditor/index.html";
        }

        Controls.WebView2 _WebView1 { get; set; }

        void initLoad()
        {
            _WebView1 = (Controls.WebView2)GetControl("WebView1");
            _WebView1.ReceiveMessage = (msg) =>
            {
                string type = msg.GetStringValue("type");
                if (type == "setValue") setContent();
            };
        }

        string _Content { get; set; }

        void setContent()
        {
            if (!_File.IsNew)
            {
                var record = RequestService.GetMeetingRecord(_File.FileId);
                if (record != null) _File.Content = record.Content;
            }
            _WebView1.Send("setValue", _File.Content);
            _Content = _File.Content;
        }

        public void Save()
        {
            _WebView1.ExcuteScript("window.getContent()", (data) =>
           {
               string content = data.GetStringValue("content");
               if (_Content == content) return;

               TaskRun("SaveRecord", true, () =>
               {
                   var record = new Dictionary<string, object>();
                   record["Content"] = content;
                   record["RecordId"] = _File.FileId;

                   var res = RequestService.SaveRecord(record, true);
                   if (res.Result)
                   {
                       _Content = content;
                   }
                   else Alert(res.Message);
               });
           });
        }

        public override void ShowLoad()
        {
            base.ShowLoad();
        }

        public override void CloseWindow()
        {
            try
            {
                Save();
                Thread.Sleep(200);
                base.CloseWindow();
                _CreateRecordlist.Remove(this);
            }
            catch(Exception ex)
            {
                Utility.LoggerProxy.Exception(Name, "CloseWindow" , ex);
            }
        }
    }
}
